﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System
Partial Public Class Service
#Region "读取全局配置"
    Public Function _Base_GetGlobalConfig(ByVal USession As String, ByRef BInfo As FConfig, ByRef ErrString As String) As Boolean Implements IService._Base_GetGlobalConfig
        Return True
    End Function
#End Region
#Region "读取单据配置"
    Public Function _Base_GetBillPrintConfig(ByVal USession As String, ByVal FBType As String, ByRef FPCFG As FBillPrintCFG, ByRef ErrString As String) As Boolean Implements IService._Base_GetBillPrintConfig
        If DControl._GetUserBySession(USession, ErrString) = False Then
            ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        Dim sqlstr As String = String.Empty
        Dim ds As New DataSet
        Dim FUinf As New FUserInfo
        If GetUserInfoBySession(USession, FUinf, ErrString) = False Then
            Return False
        End If
        If FUinf.id < 0 Or FUinf Is Nothing Then
            ErrString = "取用户信息失败!"
            Return False
        End If
        FPCFG = New FBillPrintCFG
        Dim FSType As String = String.Empty
        Select Case FBType
            Case "Property"
                '--物业单
                FSType = "PropPRINT"
            Case "Cash"
                '--现金收据
                FSType = "CashPRINT"
            Case "Product"
                '--商品收据
                FSType = "ProdPRINT"
            Case "Water"
                '--水费
                FSType = "WaterPRINT"
            Case Else
                ErrString = "参数不正确!"
                Return False
        End Select

        '--左边距
        sqlstr = String.Format("select * from t_UserConfig where FUid={0} and FGroup='{1}' and FKey='FMargin'", FUinf.id, FSType)
        If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
            Return False
        End If
        If ds.Tables.Count < 1 Then
            ErrString = "内部服务器错误"
            Return False
        End If

        If ds.Tables(0).Rows.Count < 1 Then
            FPCFG.FMarginL = 0
            FPCFG.FMarginR = 0
            FPCFG.FMarginT = 0
            FPCFG.FMarginB = 0
        Else
            FPCFG.FMarginL = ds.Tables(0).Rows(0).Item("FValue1")
            FPCFG.FMarginR = ds.Tables(0).Rows(0).Item("FValue2")
            FPCFG.FMarginT = ds.Tables(0).Rows(0).Item("FValue3")
            FPCFG.FMarginB = ds.Tables(0).Rows(0).Item("FValue4")
        End If
        '--纸张参数
        sqlstr = String.Format("select * from t_UserConfig where FUid={0} and FGroup='{1}' and FKey='FPrintset'", FUinf.id, FSType)
        If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
            Return False
        End If
        If ds.Tables.Count < 1 Then
            ErrString = "内部服务器错误"
            Return False
        End If
        If ds.Tables(0).Rows.Count < 1 Then
            FPCFG.FPaperDirect = "V"
            FPCFG.FPaperName = "A4"
            FPCFG.FPrinterName = ""
        Else
            FPCFG.FPaperDirect = ds.Tables(0).Rows(0).Item("FValue3")
            FPCFG.FPaperName = ds.Tables(0).Rows(0).Item("FValue1")
            FPCFG.FPrinterName = ds.Tables(0).Rows(0).Item("FValue2")
        End If
        Return True
    End Function
#End Region
#Region "设置单据打印配置"
    Public Function _Base_SetBillConfig(ByVal USession As String, ByVal FBType As String, ByVal PInfo As FBillPrintCFG, ByRef ErrString As String) As Boolean Implements IService._Base_SetBillConfig
        If DControl._GetUserBySession(USession, ErrString) = False Then
            ErrString = String.Format("验证用户失败!{0}错误信息:{1}", vbCrLf, ErrString)
            Return False
        End If
        Dim sqlstr As String = String.Empty
        Dim ds As New DataSet
        Dim FUinf As New FUserInfo
        If GetUserInfoBySession(USession, FUinf, ErrString) = False Then
            Return False
        End If
        If FUinf.id < 0 Or FUinf Is Nothing Then
            ErrString = "取用户信息失败!"
            Return False
        End If
        '--检查数据
        If PInfo Is Nothing Then
            ErrString = "打印参数不能为空!"
            Return False
        End If
        Try
            Dim FSType As String = String.Empty
            Select Case FBType
                Case "Property"
                    '--物业单
                    FSType = "PropPRINT"
                Case "Cash"
                    '--现金收据
                    FSType = "CashPRINT"
                Case "Product"
                    '--商品收据
                    FSType = "ProdPRINT"
                Case "Water"
                    '--水费
                    FSType = "WaterPRINT"
                Case "PropertyDown"
                    FSType = "ProdPRINT"
                Case Else
                    ErrString = "参数不正确!"
                    Return False
            End Select
            sqlstr = String.Format("select count(*) as a from t_UserConfig where FGroup='{0}' and FKey='FMargin' and FUid={1}", FSType, FUinf.id)
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = "内部服务器错误"
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = "内部服务器错误"
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                '--更新
                sqlstr = String.Format("UPDATE t_UserConfig set FValue1='{0}',FValue2='{1}',FValue3='{2}',FValue4='{3}' where FGroup='{5}' and FKey='FMargin' and FUid='{4}'", PInfo.FMarginL, PInfo.FMarginR, PInfo.FMarginT, PInfo.FMarginB, FUinf.id, FSType)
            Else
                '--新增
                sqlstr = String.Format("INSERT INTO t_UserConfig(FGroup,FKey,FValue1,FValue2,FValue3,FValue4,FUid) values('{5}','FMargin','{0}','{1}','{2}','{3}',{4})", PInfo.FMarginL, PInfo.FMarginR, PInfo.FMarginT, PInfo.FMarginB, FUinf.id, FSType)
            End If
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                Return False
            End If
            '--保存打印设置
            ds = New DataSet
            sqlstr = String.Format("select count(*) as a from t_UserConfig where FGroup='{0}' and FKey='FPrintset' and FUid={1}", FSType, FUinf.id)
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = "内部服务器错误"
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = "内部服务器错误"
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                '--更新
                sqlstr = String.Format("UPDATE t_UserConfig set FValue1='{0}',FValue2='{1}',FValue3='{2}' where FGroup='{4}' and FKey='FPrintset' and FUid={3}", PInfo.FPaperName, PInfo.FPrinterName, PInfo.FPaperDirect, FUinf.id, FSType)
            Else
                '--新增
                sqlstr = String.Format("INSERT INTO t_UserConfig(FGroup,FKey,FUid,FValue1,FValue2,FValue3) values('{4}','FPrintset','{0}','{1}','{2}','{3}')", FUinf.id, PInfo.FPaperName, PInfo.FPrinterName, PInfo.FPaperDirect, FSType)
            End If
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                Return False
            End If
            Return True
        Catch ex As Exception
            ErrString = String.Format("保存参数失败!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "检查版本"
    Public Function _BASE_CheckVer() As String Implements IService._BASE_CheckVer
        Dim s As String = IO.File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory & "\ver.df")
        Return s.Trim
    End Function
#End Region
#Region "呼吸"
    Public Function _BASE_Breath(ByRef FUinfo As FUserInfo, ByRef ErrString As String) As Boolean Implements IService._BASE_Breath
        If FUinfo Is Nothing Then
            ErrString = "参数不正确"
            Return False
        End If
        Try
            If FVerifyUser(FUinfo.FSession, ErrString) = False Then
                Return False
            End If
            Dim NSession As String = CControl.MD5(Now.ToString & FUinfo.ToString & FUinfo.id.ToString("00000.00000")).ToUpper
            Dim sqlstr As String = String.Format("update t_User set FSession='{0}',FBreathTime=GETDATE() where FSession='{1}' and id={2}", NSession, FUinfo.FSession, FUinfo.id)

            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = "验证用户失败!" & ErrString
                Return False
            Else
                FUinfo.FSession = NSession
                Return True
            End If
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "取单据流水号"
    Private Function GetBillMaxNumber(ByVal FBType As Integer, ByVal FOrgid As Integer, ByRef FMid As Integer, ByRef ErrString As String) As Boolean
        Try
            Dim sqlstr As String = String.Format("select count(*) as a from Sys_BillNo where FBillType ={0} and FOrgid={1}", FBType, FOrgid)
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("a") > 0 Then
                '--存在
                '--取单号
                sqlstr = String.Format("update Sys_BillNo SET FCode=FCode + 1 output inserted.FCode where FBillType ={0} and FOrgid={1}", FBType, FOrgid)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = "内部服务器错误"
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = "取流水号错误"
                    Return False
                Else
                    FMid = ds.Tables(0).Rows(0).Item("FCode")
                    Return True
                End If
            Else
                '--不存在
                sqlstr = String.Format("insert into Sys_BillNo(FOrgid,FBillType,FCode) values({0},{1},150)", FOrgid, FBType)
                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    Return False
                Else
                    FMid = 1
                    Return True
                End If
            End If

            'If ds.Tables(0).Rows.Count < 1 Then
            '--修改日期,归零
            'sqlstr = "update Sys_BillNo SET FCode=1,FDate=GETDATE() output inserted.FCode where FBillType =" & FBType
            'ds = New DataSet
            'If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
            '    Return False
            'Else
            '    If ds.Tables.Count < 1 Then
            '        ErrString = "内部服务器错误"
            '        Return False
            '    End If
            '    If ds.Tables(0).Rows.Count < 1 Then
            '        ErrString = "内部服务器错误"
            '        Return False
            '    End If
            '    FMid = ds.Tables(0).Rows(0).Item("FCode")
            '    Return True
            'End If
            '    ErrString = "取流水号错误"
            '    Return False
            'Else
            '    '--返回ID
            '    FMid = ds.Tables(0).Rows(0).Item("FCode")
            '    Return True
            'End If
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
End Class
